{% from "macros.jinja" import gradient_background %}
{% extends "job_result.html" %}

{% block job_result %}
<div class="row">
<div class="col-lg-12">
  <h3> Minions:
  </h3>
  <div class="list-group">
    {% for minion in job.info.Minions %}
    {% set minion_job = job.return.get(minion, {'level': None}) %}

      {% if minion_job['status'] != 'error'  %}
        {% set total = minion_job['steps'] | length %}
        {% set success = minion_job['highstate'][True] | length %}
        {% set changes = minion_job['highstate']['changes'] | length %}
        {% set warning = minion_job['highstate'][None] | length %}
        {% set requirements = minion_job['highstate']['requirement_failed'] | length %}
        {% set errors = minion_job['highstate'][False] | length %}

        {% if minion_job['level'] == 2 %}{% set level="danger" %}{% elif minion_job['level'] == 1 %}{% set level="warning" %}{% else %}{% set level="success" %}{% endif %}
        <a href="#result-{{minion}}" class="list-group-item list-group-item-{{level}}" style="{{ gradient_background(total, success, changes, warning, errors + requirements) }}">{{ minion }}</a>

      {% else %}
        <a href="#result-{{minion}}" class="list-group-item list-group-item-danger">{{ minion }}</a>
      {% endif %}


    {% endfor %}
  </ul>
</div>
</div><!-- /.row -->

{% for minion in job.info.Minions %}

    {% set minion_job = job.return.get(minion) %}

    {% if minion_job == None %}
      <div class="col-lg-12" id="result-{{minion}}">
          <div class="panel panel-warning">
            <div class="panel-heading">
              <h3>{{hiding_control('hide_control_minion')|safe }} {{ minion }} warning</h3>
            </div>
            <div class="panel-body">
              <p>No return for this minion, minion is down or return is not yet available</p>
            </div>
          </div>
      </div>
    {% else %}

      {% if minion_job.status == 'error' %}

        <div class="col-lg-12" id="result-{{minion}}">

            <div class="panel panel-danger">
              <div class="panel-heading">
                <h3>{{hiding_control('hide_control_minion')|safe }} {{ minion }} error</h3>
              </div>
              <div class="panel-body">
                <pre>{{ minion_job.error|trim() }}</pre>
              </div>
            </div>
        </div>


      {% else %}

        {% set total = minion_job['steps'] | length %}
        {% set success = minion_job['highstate'][True] | length %}
        {% set changes = minion_job['highstate']['changes'] | length %}
        {% set warning = minion_job['highstate'][None] | length %}
        {% set requirements = minion_job['highstate']['requirement_failed'] | length %}
        {% set errors = minion_job['highstate'][False] | length %}


        {% if minion_job['level'] == 2 %}{% set level="danger" %}{% elif minion_job['level'] == 1 %}{% set level="warning" %}{% else %}{% set level="success" %}{% endif %}
        <div class="col-lg-12" id="result-{{minion}}">
        <div class="panel panel-{{ level }}">
          <div class="panel-heading">
            <h3 class="hide_control">{{hiding_control('hide_control_minion', level=='success')|safe }} {{ minion }} - {{ total }} steps: {{ errors }} in errors, {{ requirements }} requirements failed, {{ warning }} in warning, {{ changes }} changes and {{ success }} in success. </h3>
          </div>
          <div class="panel-body" {% if level=='success' %}style="display: none"{% endif %}>
            {{ format_output(minion_job['highstate'][False], 'Errors', 'danger', total)|safe }}
            {{ format_output(minion_job['highstate']['requirement_failed'], 'Requirements failed', 'danger', total, True)|safe }}
            {{ format_output(minion_job['highstate'][None], 'Warning (changes)', 'warning', total)|safe }}
            {{ format_output(minion_job['highstate']['changes'], 'Changes', 'info', total)|safe }}
            {{ format_output(minion_job['highstate'][True], 'Success', 'success', total, True)|safe }}
          </div>
        </div>
        </div>

      {% endif %}
    {% endif %}
{% endfor %}
{% endblock %}


{% block scripts %}
<script type="text/javascript">
$(function() {
  {% if job.status != 'running' %}

      $("a.list-group-item i.hide_control").click(function(event) {
        $(this).parents("a.list-group-item").first().find(".sync-status-hidden").toggle();
        $(this).parent().find("i.fa").toggle();
      });
      $("div.panel-heading i.hide_control").click(function(event) {
        $(this).parents("div.panel").first().find("div.list-group").toggle();
        $(this).parent().find("i.fa").toggle();
      });

      $("div.panel-heading i.hide_control_minion").click(function(event) {
        $(this).parents("div.panel").first().find(".panel-body").toggle();
        $(this).parent().find("i.fa").toggle();
      });

  {% endif %}
});
</script>
{% endblock %}
